package com.twu.biblioteca;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: Joe
 * Date: 11/07/13
 * Time: 3:16 PM
 * To change this template use File | Settings | File Templates.
 */
public class LibraryIO {

    public BufferedReader bufferRead;

    //For testing purposes so I can simulate multipe next user inputs.
    public String[] multipleEntries;
    public int nextItemToRead;
    public boolean testing;


    public LibraryIO(InputStreamReader input){
        this.bufferRead  = new BufferedReader(input);

    }
    //for testing single next inputs
    public LibraryIO(String input){
        this.setNextUserInput(input);
    }
    //for testing
    public LibraryIO(String[] inputs){
        testing=true;
        this.multipleEntries = inputs;
        this.setNextUserInput(multipleEntries[0]);

        if(inputs.length >1)
            this.nextItemToRead = 1;
        else
            this.nextItemToRead = 0;
    }

    //for testing
    public void setNextUserInput(String input){
        InputStream is = new ByteArrayInputStream(input.getBytes());
        this.bufferRead = new BufferedReader(new InputStreamReader(is));
    }

    public String ReadLine(){
        String result ="";
        try {
            result =  this.bufferRead.readLine();
        } catch (IOException e) {
            this.printToDisplay(e.getMessage());
        }

        //testing
        if (this.testing=true){
            if(nextItemToRead <= (this.multipleEntries.length-1))
            {
                this.setNextUserInput(multipleEntries[nextItemToRead]);
                this.nextItemToRead++;
            }
        }
        return result;
    }

    public static String printToDisplay(String value){
        System.out.println(value);
        return value;
    }
}
